package com.indusfo.spc.controller;


import com.indusfo.spc.aspect.SystemControllerLog;
import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.pojo.InspectWork;
import com.indusfo.spc.pojo.InspectWorkDetail;
import com.indusfo.spc.service.InspectWorkService;
import com.indusfo.spc.vo.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 点检作业控制层
 */
@Controller
@RequestMapping("/service/inspectWork")
public class InspectWorkController {

    @Autowired
    private InspectWorkService inspectWorkService;

    /**
     * 查询
     * @return com.indusfo.spc.vo.JSONObject
     */
    @RequestMapping(value="/queryInspectWork")
    @SystemControllerLog(description="查询")
    @ResponseBody
    public JSONObject queryInspectWork(InspectWork inspectWork,String startDate,String endDate) {
        JSONObject json = inspectWorkService.selectInspectWork(inspectWork,startDate,endDate);
        return json;
    }

    /**
     * 新增
     * @return com.indusfo.spc.vo.JSONObject
     */
    @RequestMapping(value="/insertInspectWork")
    @SystemControllerLog(description="新增")
    @ResponseBody
    public JSONObject insertInspectWork(InspectWork inspectWork,Integer[] qualifiedIds) {
        try{
            JSONObject json = inspectWorkService.insertInspectWork(inspectWork,qualifiedIds);
            return json;
        } catch (GlobalException e) {  //这里只捕获自定义异常
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 修改
     * @return
     */
    @RequestMapping(value = "/updateInspectWork")
    @SystemControllerLog(description="修改")
    @ResponseBody
    public JSONObject updateInspectWork(InspectWork inspectWork,Integer[] qualifiedIds){
        try{
            JSONObject json=inspectWorkService.updateInspectWork(inspectWork,qualifiedIds);
            return json;
        } catch (GlobalException e) {  //这里只捕获自定义异常
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 启用/停用
     * @return com.indusfo.spc.vo.JSONObject
     */
    @RequestMapping(value="/definedInspectWork")
    @SystemControllerLog(description="启用/停用")
    @ResponseBody
    public JSONObject definedInspectWork(Long[] inspectWorkIds,Integer[] auditStates, Integer dataState) {
        JSONObject json = inspectWorkService.definedInspectWork(inspectWorkIds,auditStates, dataState);
        return json;
    }

    /**
     * 删除
     * @return com.indusfo.spc.vo.JSONObject
     */
    @RequestMapping(value="/deleteInspectWork")
    @SystemControllerLog(description="删除")
    @ResponseBody
    public JSONObject deleteInspectWork(Long[] inspectWorkIds,Integer[] auditStates, Integer dataState) {
        dataState = 2;
        JSONObject json = inspectWorkService.definedInspectWork(inspectWorkIds,auditStates, dataState);
        return json;
    }

    /**
     * 审核/反审核
     * @return com.indusfo.spc.vo.JSONObject
     */
    @RequestMapping(value="/auditInspectWork")
    @SystemControllerLog(description="审核/反审核")
    @ResponseBody
    public JSONObject auditInspectWork(Long[] inspectWorkIds, Integer[] auditStates, Integer auditState) {
        try{
            JSONObject json = inspectWorkService.auditInspectWork(inspectWorkIds, auditStates ,auditState);
            return json;
        } catch ( GlobalException e) {
            e.printStackTrace();
            return JSONObject.build(403, e.getMessage());

        }
    }

    /**
     * 查询指定设备的点检项目
     * @param inspectWork
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryInspectByDevice")
    public JSONObject queryInspectByDevice(InspectWork inspectWork){
        return inspectWorkService.queryInspectByDevice(inspectWork);
    }

    /**
     * 查询设备点检作业明细
     * @param inspectWorkDetail
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryInspectWorkDetail")
    public JSONObject queryInspectWorkDetail(InspectWorkDetail inspectWorkDetail){
        return inspectWorkService.queryInspectWorkDetail(inspectWorkDetail);
    }


    /**
     * 新增设备点检作业明细时查询点检项目
     * @param inspectWork
     * @return
     */
    @ResponseBody
    @RequestMapping("/filterInspectList")
    public JSONObject filterInspectList(InspectWork inspectWork){
        return inspectWorkService.filterInspectList(inspectWork);
    }
    
}
